/**
 * 
 */
package com.yuanjiu.service.jiuxian;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuanjiu.common.util.Global;
import com.yuanjiu.common.util.HtpClientUtils;
import com.yuanjiu.dao.jiuxian.CategoryDao;
import com.yuanjiu.dao.yuanjiu.DataMergeDao;
import com.yuanjiu.pojo.jiuxian.JiuxianComment;
import com.yuanjiu.pojo.jiuxian.JiuxianSpecObject;
import com.yuanjiu.pojo.yuanjiu.CategoryIdName;
import com.yuanjiu.pojo.yuanjiu.CategoryObject;
//import com.yuanjiu.webmagic.JiuxianOutput;
//import com.yuanjiu.webmagic.PhantomJsDownloader;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.PlainText;
import us.codecraft.webmagic.selector.Selectable;

/**
 * @author shiba
 *
 */
@Service
public class JiuxianData {
	
	private Logger logger = Logger.getLogger(JiuxianData.class);
	
	public static int itemcount = 0;

	private static Map<String, Map<String, Integer>> brandList=new HashMap<String, Map<String, Integer>>();

//	@Autowired
//	private CategoryDao categoryDao;
	@Autowired
	private DataMergeDao dataMergeDao;
	
	@Autowired
	private JiuxianOutput jiuxianOutput;

	public void process(String... param) {
		
//		System.setProperty("phantomjs.binary.path", "C:/Users/shiba/Desktop/phantomjs-1.9.7-windows/phantomjs-1.9.7-windows/phantomjs.exe");
//		System.getProperties().setProperty("webdriver.chrome.driver", "D:/ProgramInstall/java/jdk8u112/lib/chromedriver.exe");

		// 获取分类保存在内存中
		if (brandList.isEmpty()) {
			try {
				//获取一级分类
				List<CategoryIdName> categoryIdNames = dataMergeDao.getCategoryName();
				for (CategoryIdName tmp : categoryIdNames) {
					//获取二级分类-品牌
					List<CategoryObject> categoryObjects = dataMergeDao.getCategories(tmp.getId()); 
					HashMap<String, Integer> categoryObjects2=new HashMap<String, Integer>();
					for(CategoryObject categoryObject:categoryObjects){
						categoryObjects2.put(categoryObject.getName(), categoryObject.getId());
					}
					//保存到内存中
					brandList.put(tmp.getCat(), categoryObjects2);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//抓取商品
		Spider.create(new DataProcess(brandList))
		.addUrl(param).addPipeline(jiuxianOutput)
		//.setDownloader(new PhantomJsDownloader("C:/Users/shiba/Desktop/phantomjs-1.9.7-windows/phantomjs-1.9.7-windows/phantomjs.exe","C:/Users/shiba/Desktop/phantomjs-1.9.7-windows/phantomjs-1.9.7-windows/crawl.js"))
		.thread(6).run();
	}

}


class DataProcess implements PageProcessor {
	
	private Logger logger = Logger.getLogger(DataProcess.class);
	
	public DataProcess(Map<String, Map<String, Integer>> para){
		brandList=para;
	}
	//保存品牌
	private static Map<String, Map<String, Integer>> brandList;
	private Site site = Site.me().setTimeOut(60000).setRetrySleepTime(3000).setRetryTimes(5).setSleepTime(5000);
	@Override
	public void process(Page page) {
		//首页，分类页面url加入待抓取队列
		if (page.getUrl().get().equals("http://www\\.jiuxian\\.com")) {
			// 白酒
			page.addTargetRequest(
					new Request(page.getHtml().xpath("//li[@id='_nowactstr2']/div/h3/a/@href").get()).setPriority(80));
			// 葡萄酒
			page.addTargetRequest(
					new Request(page.getHtml().xpath("//li[@id='_nowactstr3']/div/h3/a/@href").get()).setPriority(80));
			// 洋酒
			page.addTargetRequest(
					new Request(page.getHtml().xpath("//li[@id='_nowactstr4']/div/h3/a/@href").get()).setPriority(80));
			// 啤酒
			page.addTargetRequest(new Request(page.getHtml().xpath("//li[@id='_nowactstr5']/div/h3/a[1]/@href").get())
					.setPriority(80));
			// 黄酒
			page.addTargetRequest(new Request(page.getHtml().xpath("//li[@id='_nowactstr5']/div/h3/a[2]/@href").get())
					.setPriority(80));
			// 养生酒
			page.addTargetRequest(new Request(page.getHtml().xpath("//li[@id='_nowactstr5']/div/h3/a[3]/@href").get())
					.setPriority(80));
		}
		// 列表页
		if (page.getUrl().regex("(http://list.jiuxian.com/\\d-0-0-0-0-0-0-0-0-0-0-0.htm.*)").match()) {
			List<Selectable> list = page.getHtml()
					.xpath("//div[@class='proListSearch']/ul/li").nodes();
			for (Selectable tmp : list) {
				// 详情页优先级高，遍历页面元素后再访问下一页, 添加详情页url
				page.addTargetRequest(new Request(tmp.xpath("/li/div[2]/div/a/@href").get()).setPriority(150));
			}
			String nextPage = page.getHtml().xpath("//div[@class='fanye clearfix']/a[@class='nextpage']/@href").get();
			// 添加下一页url
			if (StringUtils.isNotEmpty(nextPage)) {
				page.addTargetRequest(new Request(nextPage).setPriority(100));
			}
		}
		
		// 详情页
		if (page.getUrl().regex("(http://www\\.jiuxian\\.com/goods-[0-9]+\\.html.*)").match()) {
			JiuxianSpecObject jiuxianSpecObject = new JiuxianSpecObject();
			//获取id
			String id = page.getUrl().get();
			id=id.substring(id.indexOf("-") + 1, id.lastIndexOf("."));
			jiuxianSpecObject.setId(Long.valueOf(id));
			//获取页面
			Html nowHtml = page.getHtml();
			//获取一级分类
//			String categoryone=nowHtml.xpath("/html/body/div[5]/div[2]/a[2]/text()").get();
			String categoryone=nowHtml.$("body > div.w1200 > div.dGuide > a:nth-child(3)","text").get();
			Integer catid=null;
			Map<String, Integer> categoryObjects=brandList.get(categoryone);
			//一级分类不存在，直接返回
			if(categoryObjects==null){
				return;
			}
			//获取品牌名称
			String categorytwo=nowHtml.xpath("//body/div[@class='w1200']/div[@class='dGuide']/a[3]/text()").get();
			if(StringUtils.isEmpty(categorytwo)){
				categorytwo = nowHtml.$("body > div.w1200 > div.dGuide > a:nth-child(5)", "text").get();
//				/html/body/div[5]/div[1]/a[3]
			}
			catid=categoryObjects.get(categorytwo);
			if(catid==null){
				categorytwo="其他";
				catid=categoryObjects.get(categorytwo);
			}
			//设置品牌名称Id
			jiuxianSpecObject
					.setCategory(catid);
			//获取标题
			jiuxianSpecObject.setTitle(nowHtml
					.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[1]/h1/text()")
					.get());
			//获取价格
//			 type: 'post',
//		     url: '/pro/selectProActByProId.htm?t=' + getCurDate(),
//		     data: {
//		         'proId': goodsId,
//		         'resId': areaId
//		     },
			String priceUrl=Global.getJiuxianUrl()+"pro/selectProActByProId.htm?t="+System.currentTimeMillis();
			Map<String,String> map=new HashMap<String,String>();
			map.put("proId", id);
			String result=HtpClientUtils.doPost(priceUrl, map);
//			ObjectMapper objectMapper=new ObjectMapper();
			JsonNode node = null;
			try {
				node = new ObjectMapper().readTree(result);
			} catch (Exception e2) {
				e2.printStackTrace();
				System.out.println("result:"+result);
				logger.error("result: "+result);
			}
			String valuesuggest=node.path("act").path("shopPrice").asText();
			String valuesell=node.path("act").path("nowPrice").asText();
			jiuxianSpecObject.setValuesuggest(Double.valueOf(valuesuggest));
			jiuxianSpecObject.setValuesell(Double.valueOf(valuesell));
			
			// 建议价格存在
//			String valuesuggest = nowHtml.css("#price_and_promo > div.infoItems.priJXBox > div.infoCon.infoPrijx > del")
////					.xpath("//div[@id='price_and_promo']/div[1]/div[2]/del/text()")
//					.get();
//			
//			//有促销价存在的情况下
//			if (valuesuggest!=null&&!valuesuggest.trim().equals("")) {
//				valuesuggest = valuesuggest.substring(1);
//
//				jiuxianSpecObject.setValuesuggest(Double.valueOf(valuesuggest));
//
//			//只有酒仙价的情况下
//			}else{
//				valuesuggest = nowHtml.css("#nowPrice > span.pri > strong")
////						.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[2]/div[2]/div[2]//strong/text()")
//						.get();
//				if (valuesuggest!=null&&!valuesuggest.trim().equals("")) {
//
//					jiuxianSpecObject.setValuesuggest(Double.valueOf(valuesuggest));
//				}
//			}
//
//			
//			//售价
//			String valuesell = nowHtml
//					.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[2]/div[2]/div[2]//strong/text()")
//					.get();
//			
//			jiuxianSpecObject.setValuesell(Double.valueOf(valuesell));
			////////////////////////////// 剩余时间存在
			/**
			if (nowHtml
					.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[@class='infoWrap comSummary clearfix']/div[2]/div[2]/span[@class='tim']/@style")
					.get().equals("")) {

			}*/
			/////////////////////////////
			// 手机专享价存在
			String nowm = nowHtml
					.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[@class='infoWrap comSummary clearfix']/div[3]/div[@class='infoCon infoProm']/ul/li[@class='promItem mobilePri']")
					.get();
			if (nowm!=null&&!nowm.equals("")) {

				String vmString = nowHtml
						.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[@class='infoWrap comSummary clearfix']/div[3]/div[@class='infoCon infoProm']/ul/li[@class='promItem mobilePri']/em[@class='pri-m']/text()")
						.get();
				vmString = vmString.substring(1);
				Double dm = Double.valueOf(vmString);
				jiuxianSpecObject.setValuep(dm);
			}
			
			// 销量
			String sell = nowHtml.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[@class='salesScoreGold']//li[@class='comSales']//em/text()")
					.get();
			if(sell!=null&&sell!=""){
				jiuxianSpecObject.setSell(Long.valueOf(sell));
			}

			// 评价得分
			String comscore = nowHtml.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introInfo']/div[@class='salesScoreGold']//li[@class='comScore']//em/text()")
					.get();
			if(comscore!=null && comscore!= ""){
				jiuxianSpecObject.setComscore(Double.valueOf(comscore));
			}

			// 店铺名称
			String shop = nowHtml.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introOther']//span/text()")
					.get();
			//是否自营
			String shopOfficial=nowHtml.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introOther']//div[@class='introP']//i/text()")
					.get();
			if(shop!=null && shop!=""){
				if(shopOfficial!=null && !shopOfficial.equals("")){
					shop=shop+"-"+shopOfficial;
				}
				jiuxianSpecObject.setShop(shop);
			}
			
			// 商品编号
			String goodNo = nowHtml
					.xpath("//body/div[@class='w1200']/div[@class='dIntro clearfix']/div[@class='introShow']//div[@class='goodsNo']/span/text()")
					.get();
			if(goodNo!=null && goodNo!= ""){
				jiuxianSpecObject.setGno(goodNo);
			}
			
			// 白酒，葡萄酒等酒参数
			List<Selectable> para = nowHtml
					.xpath("/html/body/div[4]/div[4]/div[2]/div[2]/div[2]/div[1]/table/tbody/tr").nodes();

			// 净含量，酒精度，香型
			// 白酒：xxml,xx|xx',香型：xx
			// 葡萄酒：xxml,xx',香味：xx
			// 洋酒：xxml,xx'
			// 啤酒：xxL|xxml|xxML,xx|4.7%vol|8%
			// 黄酒：xxml,12°Vol|10
			// 养生酒：xxml,12°|35
			for (Selectable tmp : para) {
				String pname, pvalue;
				if ((pname = (tmp.xpath("/tr/th[1]/text()").get())).equals("净含量")) {
					pvalue = tmp.xpath("/tr/th[2]/text()").get();
					if(StringUtils.isNotEmpty(pvalue)){
						pvalue = pvalue.toLowerCase();
						if(pvalue.indexOf("ml") != -1){
							pvalue = pvalue.substring(0, pvalue.indexOf("ml"));
						}
						jiuxianSpecObject.setNet(pvalue);
					}
				}

				if ((pname = (tmp.xpath("/tr/th[1]/text()").get())).equals("酒精度")) {
					pvalue = tmp.xpath("/tr/th[2]/text()").get();
					jiuxianSpecObject.setDegree(pvalue);
				}
				if ((pname = (tmp.xpath("/tr/th[1]/text()").get())).equals("箱规")) {
					pvalue = tmp.xpath("/tr/th[2]/text()").get();
					jiuxianSpecObject.setCaron(pvalue);
				}
				String fla = tmp.xpath("/tr/th[1]/text()").get();
				pname = fla;
				if (pname.equals("香味") || pname.equals("香型")) {
					pvalue = tmp.xpath("/tr/th[2]/text()").get();
					jiuxianSpecObject.setFlavor(pvalue);
				}
			}
			//洋酒参数
			if(para==null || para.isEmpty()){
				para = nowHtml
						.xpath("/html/body/div[4]/div[5]/div[2]/div[2]/div[1]/div/div[1]/ul/li").nodes();
				for (Selectable tmp : para) {
					String pname, pvalue;
					if ((pname = (tmp.xpath("/li/span/text()").get())).contains("净含量")) {
						pvalue = tmp.xpath("/li//em/@title").get();
						if(StringUtils.isNotEmpty(pvalue)){
							pvalue = pvalue.toLowerCase();
							jiuxianSpecObject.setNet(pvalue);
						}
					}
					if ((pname = (tmp.xpath("/li/span/text()").get())).contains("酒精度")) {
						pvalue = tmp.xpath("/li//em/@title").get();
						jiuxianSpecObject.setDegree(pvalue);
					}
					if ((pname = (tmp.xpath("/li/span/text()").get())).contains("箱规")) {
						pvalue = tmp.xpath("/li//em/@title").get();
						jiuxianSpecObject.setCaron(pvalue);
					}
					String fla = tmp.xpath("/li/span/text()").get();
					pname = fla;
					if (pname.contains("香味") || pname.contains("香型")) {
						pvalue = tmp.xpath("/li//em/@title").get();
						jiuxianSpecObject.setFlavor(pvalue);
					}
				}
			}
			//若参数中没有酒精度和容量，则从标题中获取酒精度与容量
			if(jiuxianSpecObject.getDegree()==null||"".equals(jiuxianSpecObject.getDegree())){
				String tmpTitle=jiuxianSpecObject.getTitle();
				int end=tmpTitle.indexOf("°");
				if(end==-1){
					end=tmpTitle.indexOf("度");
				}
				if(end!=-1){
					tmpTitle=tmpTitle.substring(0, end);
				    Matcher matcher = Pattern.compile("[0-9]+$").matcher(tmpTitle); 
				    boolean matchresult = matcher.find();
				    if(matchresult){
					    String deg=matcher.group();
					    jiuxianSpecObject.setDegree(deg);
				    }
				}
			}
			if(jiuxianSpecObject.getNet()==null||"".equals(jiuxianSpecObject.getNet())){
				String tmpTitle=jiuxianSpecObject.getTitle();
				int end=tmpTitle.indexOf("ml");
				if(end==-1){
					end=tmpTitle.indexOf("ML");
				}
				if(end==-1){
					end=tmpTitle.indexOf("Ml");
				}
				if(end==-1){
					end=tmpTitle.indexOf("mL");
				}
				if(end!=-1){
					tmpTitle=tmpTitle.substring(0, end);
				    Matcher matcher = Pattern.compile("[0-9]+$").matcher(tmpTitle);
				    boolean matchresult = matcher.find();
				    if(matchresult){
					    String net=matcher.group();
					    jiuxianSpecObject.setNet(net);
				    }
				}
			}
			//评论顶部异步url : http://www.jiuxian.com/pro/listProductEvaluate.htm?id=43265&pagenum=1&onlyImg=true
			try {
				String urlcom="http://www.jiuxian.com/pro/listProductEvaluate.htm?id="+id+"&pagenum=1&onlyImg=true";
				String rateGood=HtpClientUtils.getCommentByPage(urlcom);
				Page page2=new Page();
				page2.setRequest(new Request(urlcom));
				page2.setUrl(new PlainText(urlcom));
				page2.setRawText(rateGood);
				Html subHtml=page2.getHtml();
				// 好评率
				String gcom= subHtml.xpath("/html/body/div[1]/div[1]/div[1]/div[1]/p/span/text()").get();
				if(gcom!=null && gcom != ""){
					jiuxianSpecObject.setGcom(Double.valueOf(gcom));
				}
				//关键词，分号分隔,关键词异步加载，在评论顶部加载之后
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// 评论
			//评论url:  http://www.jiuxian.com/pro/listProductEvaluate/ajax.htm?id=22862&pageNum=2
			//评论每页10条，抓2页即可
			String urlBase="http://www.jiuxian.com/pro/listProductEvaluate/ajax.htm?id="+id+"&pageNum=";
			try {
				String comment1=HtpClientUtils.getCommentByPage(urlBase+1);
				String comment2=HtpClientUtils.getCommentByPage(urlBase+2);
				List<JiuxianComment> comlist=new ArrayList<JiuxianComment>();
				JsonNode rootNode = new ObjectMapper().readTree(comment1);
				Iterator<JsonNode> clist = rootNode.get("commentList").elements();
				while(clist.hasNext()){
					JsonNode tmp=clist.next().path("firstComment");
					JiuxianComment jiuxianComment=new JiuxianComment();
					jiuxianComment.setId(tmp.path("comentId").asText());
					jiuxianComment.setGid(id);
					jiuxianComment.setCom(tmp.path("coment").asText());
					//setup time
					String time=tmp.get("createTime").asText();
					jiuxianComment.setTime(new Timestamp(Long.valueOf(time)));
					String grabTime = new Timestamp(System.currentTimeMillis()).toString();
					grabTime=grabTime.substring(0,grabTime.indexOf("."));
					jiuxianComment.setGrabTime(grabTime);
					comlist.add(jiuxianComment);
				}
				JsonNode nodePage2=new ObjectMapper().readTree(comment2);
				Iterator<JsonNode> clist2=nodePage2.get("commentList").elements();
				while(clist2.hasNext()){
					JsonNode tmp=clist2.next().path("firstComment");
					JiuxianComment jiuxianComment=new JiuxianComment();
					jiuxianComment.setId(tmp.get("comentId").asText());
					jiuxianComment.setGid(id);
					jiuxianComment.setCom(tmp.get("coment").asText());
					//setup time
					String time=tmp.get("createTime").asText();
					jiuxianComment.setTime(new Timestamp(Long.valueOf(time)));
					int sc=Integer.valueOf(tmp.path("comentScore").asText());
					//5分制，低于3分则为差评
					if(sc<3){
						jiuxianComment.setComtype("bad");
					}
					comlist.add(jiuxianComment);
				}
				//评论列表
				page.putField("comment", comlist);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//抓取时间
			jiuxianSpecObject.setGrabTime(new Timestamp(System.currentTimeMillis()));
			
			//抓取展示图片,url逗号分隔
			//酒仙图片服务器：c, 数字可为06,07,08,09,10
			//大小图片Url不同,大图片url最后一位为数字5，小图片url最后一位为数字1
//			List<Selectable> imgList = nowHtml.xpath("/html/body/div[5]/div[2]/div[1]/div[2]/div/ul/li").nodes();
			List<Selectable> imgList = nowHtml.$("body > div.w1200 > div.dIntro.clearfix > div.introShow > div.show-list.clearfix > div > ul > li").nodes();
			String pic="";
//			for(Selectable isave:imgList){ 
//				
//				String imgurl=isave.xpath("/li/a/img/@src").get();
//				
//				//抓取大图片，1替换成5
////				imgurl.replace("1\\.", "5\\.");
//				int start=imgurl.lastIndexOf(".")-1;
//				imgurl=imgurl.substring(0, start)+"5"+imgurl.substring(start+1);
//				
//				
//				//获取路径
//				String[] splitedPath=imgurl.split("/");
//				String level1=splitedPath[3];
//				String level2=splitedPath[4];
//				String filename=imgurl.substring(imgurl.lastIndexOf("/")+1);
//				
//				//获取图片存路径
//				String savePath=Global.getJiuXianImgPath()+"/"+level1+"/"+level2;
//				
//				File saveDirectory=new File(savePath);
//				if(!saveDirectory.exists()){
//					saveDirectory.mkdirs();
//				}
//				
//				try {
//					downloadImage(imgurl, filename, savePath);
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//				//多个图片逗号分隔
//				if(pic.equals("")){
//					pic+=imgurl;
//				}else{
//					pic=pic+","+imgurl;
//				}
//				
//			}
			for(Selectable tmp : imgList){
				String path = tmp.xpath("/li/a/img/@src").get();
				int start=path.lastIndexOf(".")-1;
				path=path.substring(0, start)+"5"+path.substring(start+1);
				if(StringUtils.isNotEmpty(pic)){
					pic+=","+path;
				}else{
					pic = path;
				}
			}
			jiuxianSpecObject.setPic(pic);
			jiuxianSpecObject.setUrl(page.getUrl().get());
			//商品详情
			page.putField("jiuObject", jiuxianSpecObject);
 
		}

	}

	public void downloadImage(String urlString, String filename, String savePath) throws Exception {
		// 构造URL
		URL url = new URL(urlString);
		// 打开连接
		URLConnection con = url.openConnection();
		// 设置请求超时为5s
		con.setConnectTimeout(5 * 1000);
		// 输入流
		InputStream is = con.getInputStream();
		// 1K的数据缓冲
		byte[] bs = new byte[1024];
		// 读取到的数据长度
		int len;
		// 输出的文件流
		File sf = new File(savePath);
		if (!sf.exists()) {
			sf.mkdirs();
		}
		OutputStream os = new FileOutputStream(sf.getPath() + "\\" + filename);
		// 开始读取
		while ((len = is.read(bs)) != -1) {
			os.write(bs, 0, len);
		}
		// 完毕，关闭所有链接
		os.close();
		is.close();
	}

	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return site;
	}

}
@Service
class JiuxianOutput implements Pipeline {
	@Autowired
	private CategoryDao categoryDao;
	@Override
	public void process(ResultItems resultItems, Task task) {
		JiuxianSpecObject jiuxianSpecObject=resultItems.get("jiuObject");
		List<JiuxianComment> comlist=resultItems.get("comment");
		if(jiuxianSpecObject!=null){
			try {
				categoryDao.insertJiuxianItem(jiuxianSpecObject);
				JiuxianData.itemcount +=1 ;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(CollectionUtils.isNotEmpty(comlist)){
			try {
				categoryDao.insertJiuxianComment(comlist);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}

}
